package top.iforum.bbs.config;


import top.iforum.bbs.modules.posts.entity.PostsColumn;
import top.iforum.bbs.modules.posts.service.IPostsColumnService;
import top.iforum.bbs.modules.sys.entity.Organization;
import top.iforum.bbs.modules.sys.entity.Teacher;
import top.iforum.bbs.modules.sys.entity.UserOrganization;
import top.iforum.bbs.modules.sys.service.IOrganizationService;
import top.iforum.bbs.modules.sys.service.ITeacherService;
import top.iforum.bbs.modules.sys.service.IUserOrganizationService;
import top.iforum.beetl.tags.dict.Dict;
import top.iforum.beetl.tags.dict.DictWithDataSet;
import top.iforum.beetl.tags.dict.InitDictable;
import top.iforum.common.utils.SpringContextHolder;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * All rights Reserved, Designed By www.iforum.top
 *
 * @version V1.0
 * @description: HTML初始化
 * @author: 张娜
 * @copyright: 2019 www.iforum.top Inc. All rights reserved.
 */

@Configuration
public class SelectOptionConfig implements InitDictable {

    public static final String ALL_TEACHER_OPTIONS = "allTeachers";
    public static final String POSTS_COLUMN = "postsColumn";

    @Autowired
    private ITeacherService teacherService;
    @Autowired
    private IUserOrganizationService userOrganizationService;
    @Autowired
    private IOrganizationService organizationService;
    @Autowired
    private IPostsColumnService postsColumnService;

    /**
     * 选项初始化
     * @return
     */
    @Override
    public Map<String, List<Dict>> initDict() {
        Map<String, List<Dict>> dictMap = new HashMap<String, List<Dict>>();

        // dict="allTeachers",
        addAllTeacherDict(dictMap, ALL_TEACHER_OPTIONS);

        // dict="postsColumn"
        addPostsColumn(dictMap, POSTS_COLUMN);

        return dictMap;
    }

    private void addPostsColumn(Map<String, List<Dict>> dictMap, String key) {
        final Iterable<? extends PostsColumn> postsColumns = postsColumnService.selectAllPostsColumn();
        for (PostsColumn column: postsColumns) {
            List<Dict> dictList = dictMap.get(key);
            Dict dict = new Dict(column.getName(), column.getCode());

            if (dictList != null) {
                dictList.add(dict);
            } else {
                dictMap.put(key, Lists.newArrayList(dict));
            }
        }
    }

    private void addAllTeacherDict(Map<String, List<Dict>> dictMap, String key) {
        final Iterable<? extends Teacher> teachers = teacherService.selectAllTeacher();
        for (Teacher teacher : teachers) {
            List<Dict> dictList = dictMap.get(key);

            // 构造一个Dict 词条
            DictWithDataSet dict = new DictWithDataSet(teacher.getRealname(), teacher.getId());
            String schoolName = null;
            String schoolId = null;
            Map<String, Object> selectMap = new HashMap<>(1);
            selectMap.put("user_id", teacher.getId());
            List<UserOrganization> userOrganizations = userOrganizationService.selectByMap(selectMap);

            if (userOrganizations!=null && userOrganizations.size() ==1) {
                Organization school = organizationService.selectById(userOrganizations.get(0).getOrganizationId());
                schoolName = school.getName();
                schoolId = school.getId();
            }

            Map<String, Object> dataset = new HashMap<>(2);
            dataset.put("schoolName", schoolName);
            dataset.put("schoolId", schoolId);
            dict.setDataset(dataset);

            if (dictList != null) {
                dictList.add(dict);
            } else {
                dictMap.put(key, Lists.newArrayList(dict));
            }
        }
    }

}
